<template>
  <div class="WxTagDialog">
    <!--  修改、添加标签组弹窗  -->
    <div v-if="dialogOpen">
      <el-dialog title="修改标签/组" :visible.sync="dialogOpen" :close-on-click-modal="false">
        <el-form :model="form" @submit.native.prevent>
          <el-form-item label="标签组：" :label-width="formLabelWidth">
            <span>{{ form.groupName }}</span>
            <i class="el-icon-edit" @click="editName(true)"/>
            <!--            <el-input v-model="form.groupName" maxlength="15" show-word-limit/>-->
          </el-form-item>
          <el-form-item label="标签：" :label-width="formLabelWidth">
            <div class="tag-group al-wx-tags">
              <div class="wx-tag" v-for="(tag,i) in form.tags" :key="tag.id">
                {{ tag.name }}
                <i class="el-icon-edit" @click="editName(false,tag)"/>
                <i class="el-icon-delete" @click="deleteTag(tag,i)"/>
              </div>
            </div>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth">
            <div class="tag-foot">
              <span style="font-size:12px;">标签数量：{{ form.tags.length }}</span>
              <div class="add-tag-btn">
                <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput"
                          maxlength="15" show-word-limit size="small" placeholder="输入标签名称"
                          @keyup.enter.native="addTagConfirm"/>
                <el-button v-else class="button-new-tag" size="small" @click="showInput">添加标签</el-button>
              </div>
              <el-button class="sure" v-if="inputVisible" type="primary" size="small" @click="addTagConfirm">确定
              </el-button>
              <el-button class="sure" v-if="inputVisible" size="small" @click="addTagCancel">取消</el-button>
            </div>
          </el-form-item>
        </el-form>

        <el-dialog width="30%" :title="edit.title" :visible.sync="innerVisible" append-to-body>
          <el-input v-model="edit.text" maxlength="15" show-word-limit/>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="editSubmit">确定</el-button>
            <el-button @click="innerVisible=false">取消</el-button>
          </div>
        </el-dialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import WxTooltip from "@/components/wxqy/wxTooltip/wxTooltip";
import WxTag from "@/components/wxqy/wxTag/wxTag";
import WxLink from "@/components/wxqy/wxLink/wxLink";

/**
 * @param rename 重命名
 * @param tagSubmit 提交函数 返回：数据，callback
 * @param tagRemove 删除标签
 */
export default {
  name: "WxTagDialog",
  components: {WxLink, WxTag, WxTooltip},
  props: ['refreshTag'],
  data() {
    return {
      innerVisible: false,
      dialogOpen: false,
      addGroupOpen: false,
      formLabelWidth: '120px',
      inputVisible: false,
      inputValue: '',
      form: {},
      type: null,
      edit: {}
    }
  },
  methods: {
    open(type, data) {
      this.type = type
      this.form = data
      this.dialogOpen = true
    },
    editName(isGroup, row) {
      if (isGroup) {
        this.edit = {id: this.form.id, title: '编辑分组', text: this.form.groupName, editType: 1}
      } else {
        this.edit = {
          id: row.id, title: '编辑标签',
          text: row.name, editType: 2, groupId: this.form.id
        }
      }
      this.innerVisible = true
    },
    deleteTag(row, index) {
      this.$confirm(`确定删除标签「${row.name}」?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$emit('tagRemove', row.id, () => {
          this.form.tags.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          if (!this.form.tags.length) {
            this.dialogOpen = false
          }
        })
      })
    },
    editSubmit() {
      let e = this.edit
      this.$emit('rename', {id: e.id, name: e.text, t: e.editType, groupId: e.groupId},
        () => {
          for (let i = 0; i < this.form.tags.length; i++) {
            if (this.form.tags[i].id === e.id) {
              let tag = Object.assign(this.form.tags[i], {name: e.text})
              this.$set(this.form.tags, i, tag)
              break
            }
          }
          this.innerVisible = false
          this.$message.success(e.title + "成功")
        })
    },
    removeTag(tag, index) {
      this.$confirm('是否删除标签「' + tag.name + '」?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.tags.splice(index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      })
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      })
    },
    addTagConfirm() {
      let addTag = {groupId: this.form.id, name: this.inputValue, t: 2}
      this.$emit('tagSubmit', addTag, (id) => {
        if (this.inputValue) {
          this.form.tags.push({name: this.inputValue, id: id})
        }
        this.inputVisible = false
        this.inputValue = ''
      })
    },
    addTagCancel() {
      this.inputVisible = false
      this.inputValue = ''
    },
    onSubmit() {
      if (!this.form.groupName) {
        this.$message.error('标签组名称不能为空')
        return
      }
      this.form.type = this.type
      //提交api ,通过id>0 判断新增和编辑
      if (this.refreshTag) {
        this.refreshTag()
      }
    }
  }
}
</script>

<style scoped lang="scss">
@import "../../../assets/styles/common";

.tag-group {
  max-height: 45vh;
  overflow-y: auto;
  @include flex-start;
  flex-wrap: wrap;
}

.tag-foot {
  @include flex-start;

  span {
    margin-right: 10px;
  }

  .sure {
    margin-left: 10px;
  }
}

.add-tag-btn {
  width: 200px;
}

.wx-tag {
  margin-left: 4px;
  display: flex;
  align-items: center;
  padding: 0 5px;
  cursor: pointer;
  color: #1f2d3d;

  i {
    visibility: hidden;
    cursor: pointer;
  }
}

.wx-tag:hover {
  background-color: #e3e3e3;

  i {
    display: inline-flex;
    visibility: initial;
  }
}
</style>
